package mn.slide.laborberlin.parse;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import mn.slide.laborberlin.model.PlistModel;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class ParsePlist {

	public ArrayList<PlistModel> parsePlist(String xml) {
		final ArrayList<PlistModel> dataModels = new ArrayList<PlistModel>();
		final Document doc = XMLfromString(xml);
		final NodeList nodes_array = doc.getElementsByTagName("dict");
		Log.d("number", nodes_array.getLength() + "");
		for (int index = 0; index < nodes_array.getLength(); index++) {
			final Node node = nodes_array.item(index);
			if (node.getNodeType() == Node.ELEMENT_NODE) {
				final Element e = (Element) nodes_array.item(index);
				final NodeList nodeKey = e.getElementsByTagName("key");

				final NodeList nodeValue = e.getElementsByTagName("string");
				PlistModel model = new PlistModel();
				for (int i = 0; i < nodeValue.getLength(); i++) {

					final Element eleKey = (Element) nodeKey.item(i);
					final Element eleString = (Element) nodeValue.item(i);
					if (eleString != null) {

						String strValue = getValue(eleString, "string");
						Log.d("value", strValue);
						if (getValue(eleKey, "key").equals("title")) {
							model = new PlistModel();
							model.setTitle(strValue);
						} else if (getValue(eleKey, "key").equals("sortID")) {
							model.setSortId(strValue);
						} else if (getValue(eleKey, "key").equals("tel1Name")) {
							model.setTel1Name(strValue);
						} else if (getValue(eleKey, "key").equals("tel2Name")) {
							model.setTel2Name(strValue);
						} else if (getValue(eleKey, "key").equals("tel3Name")) {
							model.setTel3Name(strValue);
						} else if (getValue(eleKey, "key").equals("name1")) {
							model.setName1(strValue);
						} else if (getValue(eleKey, "key").equals("name2")) {
							model.setName2(strValue);
						} else if (getValue(eleKey, "key").equals("address1")) {
							model.setAddress1(strValue);
						} else if (getValue(eleKey, "key").equals("address2")) {
							model.setAddress2(strValue);
						} else if (getValue(eleKey, "key").equals("tel1")) {
							model.setTel1(strValue);
						} else if (getValue(eleKey, "key").equals("tel2")) {
							model.setTel2(strValue);
						} else if (getValue(eleKey, "key").equals("fax")) {
							model.setFax(strValue);
						} else if (getValue(eleKey, "key").equals("longtitude")) {
							model.setLongtitude(strValue);
						} else if (getValue(eleKey, "key").equals("latitude")) {
							if (strValue == null) {
								strValue = "";
							}
							model.setLatitude(strValue);
							dataModels.add(model);
						}
					}
				}
			}
		}
		return dataModels;
	}

	private Document XMLfromString(String xml) {
		Document doc = null;

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xml));
			doc = db.parse(is);
		} catch (ParserConfigurationException e) {
			System.out.println("XML parse error: " + e.getMessage());
			return null;
		} catch (SAXException e) {
			System.out.println("Wrong XML file structure: " + e.getMessage());
			return null;
		} catch (IOException e) {
			System.out.println("I/O exeption: " + e.getMessage());
			return null;
		}

		return doc;
	}

	private String getElementValue(Node elem) {
		Node kid;
		if (elem != null) {
			if (elem.hasChildNodes()) {
				for (kid = elem.getFirstChild(); kid != null; kid = kid
						.getNextSibling()) {
					if (kid.getNodeType() == Node.TEXT_NODE) {
						return kid.getNodeValue();
					}
				}
			}
		}
		return "";
	}

	private String getValue(Element item, String str) {
		NodeList n = item.getElementsByTagName(str);
		return getElementValue(n.item(0));
	}
}